{
 "metadata": {
  "name": "Interference Alignment - Alternating Minimizations"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Alternating Minimizations IA Algorithm"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This notebook simulates the Alternating Minimizations Interference Alignment algorithm."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Some initialization code"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# xxxxxxxxxx Add the parent folder to the python path. xxxxxxxxxxxxxxxxxxxx\n",
      "import sys\n",
      "import os\n",
      "pyphysim_folder = \"~/cvs_files/pyphysim/\"\n",
      "pyphysim_folder = os.path.expanduser(pyphysim_folder)\n",
      "sys.path.append(pyphysim_folder)\n",
      "# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
      "\n",
      "# Import the simulation runner\n",
      "from apps.simulate_ia import AlternatingSimulationRunner\n",
      "from util import simulations\n",
      "\n",
      "# We will use clear_output to erase the progressbar after the simulation has finished.\n",
      "from IPython.display import clear_output\n",
      "\n",
      "# We will use pprint to print the simulation parameters\n",
      "from pprint import pprint"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Run the algorithm with 120 iterations (in parallel)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The configuration of the simulation is in the 'ia_config_file.txt' file. You can edit and run the cell below to update the configuration file."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%file ia_config_file_120.txt\n",
      "\n",
      "[Scenario]\n",
      "        SNR = [  0.   5.  10.  15.  20.  25.  30.]\n",
      "        M = 4\n",
      "        modulator = PSK\n",
      "        NSymbs = 100\n",
      "        K = 3\n",
      "        Nr = 2\n",
      "        Nt = 2\n",
      "        Ns = 1\n",
      "[IA Algorithm]\n",
      "        max_iterations = 120\n",
      "[General]\n",
      "        rep_max = 5000\n",
      "        max_bit_errors = 10000\n",
      "        unpacked_parameters = SNR,"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "First we need to create a \"view\" of the engines. Note that you need to start the engines before calling the code below."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from IPython.parallel import Client\n",
      "cl = Client()\n",
      "dview = cl.direct_view()\n",
      "\n",
      "dview.execute('%reset')  # Reset the engines so that we don't have\n",
      "                         # variables there from last computations\n",
      "# Add the folder containing PyPhysim to the python path in all the\n",
      "# engines\n",
      "dview.execute('import sys')\n",
      "dview.execute('sys.path.append(\"{0}\")'.format(pyphysim_folder))\n",
      "\n",
      "# For the actual simulation we are better using a load balanced view\n",
      "lview = cl.load_balanced_view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "All we need to do now is creating the runner object, call its \"simulate\" method (or the simulate_in_parallel method) and save the results to a file."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "runner_120_parallel = AlternatingSimulationRunner('ia_config_file_120.txt')\n",
      "pprint(runner_120_parallel.params.parameters)\n",
      "runner_120_parallel.simulate_in_parallel(lview)\n",
      "\n",
      "# xxxxxxxxxx Get the parameters xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
      "K = runner_120_parallel.params[\"K\"]\n",
      "Nr = runner_120_parallel.params[\"Nr\"]\n",
      "Nt = runner_120_parallel.params[\"Nt\"]\n",
      "Ns = runner_120_parallel.params[\"Ns\"]\n",
      "modulator_name = runner_120_parallel.modulator.name\n",
      "# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
      "\n",
      "# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
      "# File name (without extension) for the figure and result files.\n",
      "results_filename_120_parallel = 'ia_alt_min_results_{0}_{1}x{2}({3})_120_parallel'.format(\n",
      "    modulator_name,\n",
      "    Nr,\n",
      "    Nt,\n",
      "    Ns)\n",
      "# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
      "\n",
      "# xxxxxxxxxx Save the simulation results to a file xxxxxxxxxxxxxxxxxxxx\n",
      "runner_120_parallel.results.save_to_file('{0}.pickle'.format(results_filename_120_parallel))\n",
      "# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
      "\n",
      "print \"Runned iterations: {0}\".format(runner_120_parallel.runned_reps)\n",
      "print \"Elapsed Time: {0}\".format(runner_120_parallel.elapsed_time)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Plot the results"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "results_120_parallel = simulations.SimulationResults.load_from_file('{0}.pickle'.format(\n",
      "    results_filename_120_parallel))\n",
      "\n",
      "# Get the BER and SER from the results object\n",
      "ber_parallel = results_120_parallel.get_result_values_list('ber')\n",
      "ser_parallel = results_120_parallel.get_result_values_list('ser')\n",
      "ia_iterations_parallel = results_120_parallel.params['max_iterations']\n",
      "\n",
      "# Get the SNR from the simulation parameters\n",
      "SNR_parallel = np.array(results_120_parallel.params['SNR'])\n",
      "\n",
      "# Can only plot if we simulated for more then one value of SNR\n",
      "if SNR_parallel.size > 1:\n",
      "    fig = figure(figsize=(12,9))\n",
      "    semilogy(SNR_parallel, ber_parallel, '--g*', label='BER')\n",
      "    semilogy(SNR_parallel, ser_parallel, '--b*', label='SER')\n",
      "    xlabel('SNR')\n",
      "    ylabel('Error')\n",
      "    title('Alt. Min IA Algorithm ({5} Iterations)\\nK={0}, Nr={1}, Nt={2}, Ns={3}, {4}'.format(K, Nr, Nt, Ns, modulator_name, ia_iterations_parallel))\n",
      "    legend()\n",
      "\n",
      "    grid(True, which='both', axis='both')\n",
      "    show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}